package cn.xuewei.controller;


import cn.xuewei.pachong.qiniuyun.Qiniuyun;
import cn.xuewei.response.ResultVO;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

@RestController
@RequestMapping("/v1/upload")
public class UploadController {

    @Value("${filePath}")
    private String filePath;
    /**
     * 文件上传
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("")
    public ResultVO upload(MultipartFile file) throws IOException {
        //得的文件上传的名称
        String name = file.getOriginalFilename();
        String fileName = name +"_"+System.currentTimeMillis();
        System.out.println(fileName);
        //文件上传至云存储
        InputStream inputStream =file.getInputStream();
        String protectName = "img";

        Qiniuyun qiniuyun = new Qiniuyun();
        String url = qiniuyun.upFile(inputStream,fileName,protectName);
        //上传至云存储完毕
        return ResultVO.ok(url);

        //得到文件后缀名
//        String suffix= fileName.substring(fileName.lastIndexOf("."));
//        //得到唯一文件名 UUID+后缀
//        fileName = UUID.randomUUID()+suffix;
//        System.out.println(fileName);
//        //准备保存文件的路径
//
//        SimpleDateFormat f = new SimpleDateFormat("/yyyy/MM/dd/");
//        String datePath = f.format(new Date());
//        File dirFile = new File(filePath+datePath);
//        if(!dirFile.exists()){//如果不存在则创建一个新的文件夹
//            dirFile.mkdirs();
//        }
//        //把图片放入文件夹
//        file.transferTo(new File(filePath+datePath+fileName));
        //把图片理解响应到客户端
//        return ResultVO.ok(datePath+fileName);

    }

    @CrossOrigin // 处理跨域访问
    @ResponseBody
    @RequestMapping(value = "/user")
    public Object upImage(MultipartFile file) {
        try {
            //得的文件上传的名称
            String name = file.getOriginalFilename();
            String fileName = name +"_"+System.currentTimeMillis();
            System.out.println(fileName);
            //文件上传至云存储
            InputStream inputStream =file.getInputStream();
            String protectName = "img";

            Qiniuyun qiniuyun = new Qiniuyun();
            String url = qiniuyun.upFile(inputStream,fileName,protectName);
            JSONObject jsonObject=new JSONObject();
            JSONObject jsonObject1=new JSONObject();
            System.out.println(url);
            jsonObject1.put("url",url);
            jsonObject.put("errno",0);
            jsonObject.put("data",jsonObject1);
            return jsonObject;
        } catch (Exception e) {
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("errno",1);
            jsonObject.put("message","失败信息");
            return jsonObject;
        }

    }

    /**
     * 文件删除
     * @param url
     */
    @RequestMapping("remove")
    public void remove(String url){
        new File(filePath+url).delete();
    }

}
